<%--
  Created by IntelliJ IDEA.
  User: ssx
  Date: 2022/6/29
  Time: 21:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试结果展示</title>
    <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
    <script src="../../static/layui-main/src/layui.js"></script>
    <script src="../../static/jquery-3.5.0.min.js"></script>
</head>
<body>
<%--模糊查询--%>
<form class="layui-form" action="">
    <div class="layui-inline">
        <label class="layui-form-label">测试者编号</label>
        <div class="layui-input-inline">
            <input type="text" name="testerId" placeholder="请输入测试者编号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline"><label class="layui-form-label">测试题编号</label>
        <div class="layui-input-inline">
            <input type="text" name="questionId" placeholder="请输入题目编号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="search_reset">重置</button>
        </div>
    </div>
</form>

<table id="demo" lay-filter="test"></table>

<%--头部工具栏--%>
<script type="text/html" id="toolbarDemo">
    <a class="layui-btn layui-btn-sm" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="delete">删除</a>
</script>

<%--行内工具栏--%>
<script type="text/babel" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<%--修改的弹框from--%>
<form class="layui-form" action="" id="updateUserForm" lay-filter="saveUserFormFilter" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">测试结果编号</label>
        <div class="layui-input-block">
            <input type="text" name="resultId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">测试者编号</label>
        <div class="layui-input-block">
<%--            <select name="deptId" lay-verify="" id="xiala"></select>--%>
            <input type="text" name="testerId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">测试题编号</label>
        <div class="layui-input-block">
            <input type="text" name="questionId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <input type="text" name="answer" required lay-verify="required" placeholder="选项" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formSaveUser">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<%--添加的弹框from--%>
<form class="layui-form" action="" id="addUserForm" lay-filter="saveAddUserFormFilter" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">测试者编号</label>
        <div class="layui-input-block">
            <%--            <select name="deptId" lay-verify="" id="xiala"></select>--%>
            <input type="text" name="testerId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">测试题编号</label>
        <div class="layui-input-block">
            <input type="text" name="questionId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">答案</label>
        <div class="layui-input-block">
            <input type="text" name="answer" required lay-verify="required" placeholder="选项" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formSaveUser">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

</body>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        //第一个实例
        table.render({
            elem: '#demo'
            ,id: 'demo'
            ,height: 500
            ,url: '${pageContext.request.contextPath}/tsetResultFpa/select.do' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,limits:[5,10,15,20,25,30,50]
            ,autoSort: false//禁用前端的自动排序，开启后端的服务器排序，根据客户需求
            ,toolbar: '#toolbarDemo'//开启顶部工具栏
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'resultId', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{field: 'testerId', title: '测试者编号', width:120}
                ,{field: 'questionId', title: '测试题编号', width:120, sort: true}
                ,{field: 'answer', title: '答案', width:60}
                //关联表数据展示
                /*,{field: 'testerFpa', title: '姓名', width: 100,
                    templet1: function (hhh) {    //templet自定义模板书写错误为templet1   20220630
                        return hhh.testerFpa.name
                    }
                }*/
                ,{field:'testerFpa',title: '姓名',width: 100,
                    templet: function (data){
                    if(data.testerFpa) return data.testerFpa.name;
                    return ""
                    }
                }
                ,{field:'testerFpa',title: '电话',width: 120,
                    templet: function (data){
                        return data.testerFpa.phone;
                    }
                }
                ,{field: 'testerFpa', title: '测试时间', width: 120,
                    templet: function (hhh) {
                        return hhh.testerFpa.createTime
                    }
                }
                ,{field: 'testerFpa', title: '测试计划编号', width: 120,
                    templet: function (hhh) {
                        return hhh.testerFpa.testPlanId
                    }
                }
                ,{fixed: 'right', title: '操作',width: 180, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //触发排序事件
        table.on('sort(test)', function (obj) { //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('demo', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                , where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    , order: obj.type //排序方式
                }
            });
        });
        //模糊查询
        //把查询按钮进行事件绑定
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    testerId: data.field.testerId //第一个查询条件
                    ,questionId: data.field.questionId
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
        //重置按钮进行事件绑定
        $("#search_reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    testerId: "" //第一个查询条件
                    , questionId: ""
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            //form表单的内容回显
            form.val('savepassFormFilter', {
                "result_id": data.result_id
            });
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                //添加
                case 'add':
                    layer.open({
                        title: '添加用户',
                        type: 1,
                        content: $('#addUserForm'),
                        area: ['500px', '400px']
                    });
                    //监听addUserForm表单的提交事件toolbarDemo
                    form.on('submit(formSaveUser)', function (data) {
                        $.ajax({
                            url: '${pageContext.request.contextPath}/tsetResultFpa/add.do'
                            , data: data.field
                            , dataType: "json"
                            , type: "post"
                            , success: function (redata) {
                                if (redata.code == 0) {
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                }
                                layer.msg(redata.msg);
                            }
                        });
                        return false;
                    });
                    break;
                //删除
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        var ids = [];            //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].resultId;
                        }
                        console.log(ids)
                        layer.confirm('真的删除行么', function (index) {
                            //删除数据库中
                            $.ajax({
                                url: "${pageContext.request.contextPath}/tsetResultFpa/delete.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {resultId: ids.join(",")},
                                success: function (data) {
                                    if (data.code == 0) {
                                        //关闭对话框
                                        layer.closeAll(); //疯狂模式，关闭所有层
                                        //刷新表格数据
                                        table.reload('demo');
                                    }
                                    layer.msg(data.msg);
                                }
                            });
                        })
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是
            var data = obj.data //获得当前行数据
            //console.log(data);
            var layEvent = obj.event; //获得 lay-event 对应的值
            //删除
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //删除数据库中
                    $.ajax({
                        url: "${pageContext.request.contextPath}/tsetResultFpa/delete.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {resultId: data.resultId},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');
                            }
                            layer.msg(data.msg);
                        }
                    });
                });
            } else if (layEvent === 'update') {
                layer.open({
                    title: '修改用户',
                    type: 1,
                    content: $('#updateUserForm'),
                    area: ['500px', '400px']
                });
                //form表单的内容回显
                form.val('saveUserFormFilter', {
                    "resultId": data.resultId
                    , "testerId": data.testerId
                    , "questionId": data.questionId
                    , "answer": data.answer
                });
                //监听表单的修改事件
                form.on('submit(formSaveUser)', function (data) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/tsetResultFpa/update.do'
                        , data: data.field
                        , dataType: "json"
                        , type: "post"
                        , success: function (redata) {
                            if (redata.code == 0) {
                                layer.closeAll();//关闭对话框
                                table.reload('demo', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            }
                            layer.msg(redata.msg);
                        }
                    });
                    return false;
                });
            }
        });
    });
</script>
</html>
